package babo.cd.physicalexperiment.dialog;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.bumptech.glide.Glide;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.google.gson.Gson;
import com.lxj.xpopup.core.BottomPopupView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import babo.cd.physicalexperiment.R;
import babo.cd.physicalexperiment.bean.TiaozhengzyltMqttBean;
import babo.cd.physicalexperiment.data.MyEnventData;
import babo.cd.physicalexperiment.utils.Out;
import babo.cd.physicalexperiment.view.RoundAngleImageView;

public class TiaozhengzyltDialog extends BottomPopupView {

    Context context;

    TextView tx_sure,tx_cancle;
    RoundAngleImageView iv_cutiao,iv_weitiao;
    private String cturl,wturl;
    WheelView wheelView1,wheelView2;
    LinearLayout ll1,ll2;
    long ctime=0;
    private String ctzzd,wtzzd,ctzqd,wtzqd;
    int type=0;//1是起点 0是中点
    private String deviceId;
    private Gson gson=new Gson();
    public TiaozhengzyltDialog(String deviceid,@NonNull Context context, String cturl, String wturl , String ctzzd, String ctzqd,String wtzzd, String wtzqd) {
        super(context);
        this.context=context;
        this.cturl=cturl;
        this.wturl=wturl;
        this.ctzqd=ctzqd;
        this.ctzzd=ctzzd;
        this.wtzqd=wtzqd;
        this.wtzzd=wtzzd;
        this.deviceId=deviceid;

    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_tiaozhengzylt;
    }
    public void setZhongidan(Bitmap bitmap){
        iv_weitiao.setImageBitmap(bitmap);
    }
    public void setQidian(Bitmap bitmap){
        iv_cutiao.setImageBitmap(bitmap);
    }
    @Override
    protected void onCreate() {
        super.onCreate();
        ll1=(LinearLayout)findViewById(R.id.ll1);
        ll2=(LinearLayout)findViewById(R.id.ll2);
        tx_sure=(TextView)findViewById(R.id.tx_sure);
        tx_cancle=(TextView) findViewById(R.id.tx_cancle);
        wheelView1=(WheelView)findViewById(R.id.wh1);
        wheelView2=(WheelView)findViewById(R.id.wh2);
        List<String> cts = new ArrayList<>();
        List<String> wts = new ArrayList<>();
        for (int i = 120; i >-121 ; i--) {
            cts.add(i+"cm");
        }
        for (int i = 0; i <10 ; i++) {
            wts.add((i)+"mm");
        }


        wheelView1.setAdapter(new ArrayWheelAdapter(cts));
        wheelView1.setCyclic(false);
        wheelView1.setCurrentItem(121);
        wheelView1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
              Out.out("粗调："+index+"  ==="+wheelView1.getAdapter().getItem(index).toString());
              if (type==0){
                  ctzzd=wheelView1.getAdapter().getItem(index).toString();
              }else{
                  ctzqd=wheelView1.getAdapter().getItem(index).toString();
              }


             if (index==240||index==0){
                 List<String> ctss=new ArrayList<>();
                 ctss.add("0mm");
                 wheelView2.setAdapter(new ArrayWheelAdapter(ctss));
                 wheelView2.setCurrentItem(0);
             }else{
                 wheelView2.setAdapter(new ArrayWheelAdapter(wts));
             }

            }
        });
        if (type==0){
            if (ctzzd.equals("120")||ctzzd.equals("-120")){
                List<String> wtsss = new ArrayList<>();
                wtsss.add("0mm");
                wheelView2.setAdapter(new ArrayWheelAdapter(wtsss));
            }else{
                wheelView2.setAdapter(new ArrayWheelAdapter(wts));
            }
        }else{
            if (ctzqd.equals("120")||ctzqd.equals("-120")){
                List<String> wtsss = new ArrayList<>();
                wtsss.add("0mm");
                wheelView2.setAdapter(new ArrayWheelAdapter(wtsss));
            }else{
                wheelView2.setAdapter(new ArrayWheelAdapter(wts));
            }
        }


        wheelView2.setCyclic(false);

        wheelView2.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {

//                if (System.currentTimeMillis()-ctime>1000){
//                    //todo 执行
//                    String ct=(wheelView1.getAdapter().getItem(wheelView1.getCurrentItem()).toString());
//                    Out.out("qqctA="+ct);
//                    ct=ct.replace("cm","");
//                    String wt=(wheelView2.getAdapter().getItem(wheelView2.getCurrentItem()).toString());
//                    Out.out("qqwtA="+wt);
//                    wt=wt.replace("mm","");
//
//                    Out.out("qqct=="+ct);
//                    Out.out("qqwt=="+wt);
//                    TiaozhengzyltMqttBean tiaozhengzyltMqttBean=new TiaozhengzyltMqttBean();
//                    tiaozhengzyltMqttBean.setAction("adjust");
//                    tiaozhengzyltMqttBean.setDevice_id(deviceId);
//                    tiaozhengzyltMqttBean.setExp_id("2");
//                    tiaozhengzyltMqttBean.setType((type+1));
//                    int ct2=Integer.parseInt(ct)*10;
//                    int wt2=Integer.parseInt(wt);
//                    if (ct2>0){
//                        int zhe=ct2+wt2;
//                        tiaozhengzyltMqttBean.setDistance("+"+zhe+"");
//                    }else{
//                        int zhe=ct2-wt2;
//                        tiaozhengzyltMqttBean.setDistance(zhe+"");
//                    }
//
//                    EventBus.getDefault().post(MyEnventData.getInstance("gundong@"+gson.toJson(tiaozhengzyltMqttBean)));
//                }
                if (type==0){
                    wtzzd=wheelView2.getAdapter().getItem(index).toString();
                }else{
                    wtzqd=wheelView2.getAdapter().getItem(index).toString();
                }

//                ctime=System.currentTimeMillis();
            }
        });
         doCheckCurrent(cts,wts);

        iv_cutiao=(RoundAngleImageView)findViewById(R.id.iv_cutiao);
        iv_weitiao=(RoundAngleImageView)findViewById(R.id.iv_weitiao);
        tx_cancle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                dismiss();
            }
        });

        if (!TextUtils.isEmpty(cturl)){
            Glide.with(context).load(cturl).into(iv_cutiao);
        }
        if (!TextUtils.isEmpty(wturl)){
            Glide.with(context).load(wturl).into(iv_weitiao);
        }
        setLister();

    }

    private void doCheckCurrent(List<String> cts, List<String> wts) {
        if (type==0){
            String ctzname=ctzzd+"cm";
            for (int i=0;i<cts.size();i++){
                if (cts.get(i).equals(ctzname)){
                    wheelView1.setCurrentItem(i);
                    break;
                }
            }
            String wtzname=wtzzd+"mm";
            for (int i=0;i<wts.size();i++){
                if (wts.get(i).equals(wtzname)){
                    wheelView2.setCurrentItem(i);
                    break;
                }
            }
        }else{
            String ctzname=ctzqd+"cm";
            for (int i=0;i<cts.size();i++){
                if (cts.get(i).equals(ctzname)){
                    wheelView1.setCurrentItem(i);
                    break;
                }
            }
            String wtzname=wtzqd+"mm";
            for (int i=0;i<wts.size();i++){
                if (wts.get(i).equals(wtzname)){
                    wheelView2.setCurrentItem(i);
                    break;
                }
            }
        }

    }

    private void setLister() {
        ll1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                type=1;
               ll1.setBackgroundResource(R.drawable.backxuanzhetup);
               ll2.setBackgroundResource(0);
                List<String> cts = new ArrayList<>();
                List<String> wts = new ArrayList<>();
                for (int i = 120; i >-121 ; i--) {
                    cts.add(i+"cm");
                }
                for (int i = 0; i <10 ; i++) {
                    wts.add((i)+"mm");
                }

                if (ctzqd.equals("120")||ctzqd.equals("-120")){
                    List<String> wtsss = new ArrayList<>();
                    wtsss.add("0mm");
                    wheelView2.setAdapter(new ArrayWheelAdapter(wtsss));
                    doCheckCurrent(cts,wts);
                    wheelView2.setCurrentItem(0);
                }else{
                    wheelView2.setAdapter(new ArrayWheelAdapter(wts));
                    doCheckCurrent(cts,wts);
                }

            }
        });
        ll2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                type=0;
                ll2.setBackgroundResource(R.drawable.backxuanzhetup);
                ll1.setBackgroundResource(0);
                List<String> cts = new ArrayList<>();
                List<String> wts = new ArrayList<>();
                for (int i = 120; i >-121 ; i--) {
                    cts.add(i+"cm");
                }
                for (int i = 0; i <10 ; i++) {
                    wts.add((i)+"mm");
                }

                if (ctzzd.equals("120")||ctzzd.equals("-120")){
                    List<String> wtsss = new ArrayList<>();
                    wtsss.add("0mm");
                    wheelView2.setAdapter(new ArrayWheelAdapter(wtsss));
                    doCheckCurrent(cts,wts);
                    wheelView2.setCurrentItem(0);
                }else{
                    wheelView2.setAdapter(new ArrayWheelAdapter(wts));
                    doCheckCurrent(cts,wts);
                }

            }
        });
        tx_sure.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                String ct=(wheelView1.getAdapter().getItem(wheelView1.getCurrentItem()).toString());
                Out.out("ctA="+ct);
                ct=ct.replace("cm","");
                String wt=(wheelView2.getAdapter().getItem(wheelView2.getCurrentItem()).toString());
                Out.out("wtA="+wt);
                wt=wt.replace("mm","");

                Out.out("ct=="+ct);
                Out.out("wt=="+wt);
                TiaozhengzyltMqttBean tiaozhengzyltMqttBean=new TiaozhengzyltMqttBean();
                tiaozhengzyltMqttBean.setAction("adjust");
                tiaozhengzyltMqttBean.setDevice_id(deviceId);
                tiaozhengzyltMqttBean.setExp_id("2");
                tiaozhengzyltMqttBean.setType((type+1));
                int ct2=Integer.parseInt(ct)*10;
                int wt2=Integer.parseInt(wt);
                if (ct2>0){
                    int zhe=ct2+wt2;
                    tiaozhengzyltMqttBean.setDistance("+"+zhe+"");
                }else{
                    int zhe=ct2-wt2;
                    tiaozhengzyltMqttBean.setDistance(zhe+"");
                }

                EventBus.getDefault().post(MyEnventData.getInstance("gundong@"+gson.toJson(tiaozhengzyltMqttBean)));





                String cv0=ctzzd.replace("cm","");
                String wv0=wtzzd.replace("mm","");
                String cv1=ctzqd.replace("cm","");
                String wv1=wtzqd.replace("mm","");
                if (cv0.equals("120")||cv0.equals("-120")){
                    wv0="0";
                }
                if (cv1.equals("120")||cv1.equals("-120")){
                    wv1="0";
                }
                EventBus.getDefault().post(MyEnventData.getInstance("tiaozhengmkex@"+cv0+"@"+wv0+"@"+cv1+"@"+wv1));

                dismiss();

            }
        });
    }


}
